package org.lzfto.flink.demo.business.flinkTest.flinkObjects.selector;

import java.io.Serializable;
import java.util.Objects;

import org.lzfto.flink.demo.business.orders.OrderCommon;

/**
 * 混合分组键-必须实现hasCode和equals
 */
public class OrderMixedKey implements Serializable {
	private static final long serialVersionUID = 1L;

	private String grpType;
	private String zoneName;
	private String sex;

	public OrderMixedKey() {

	}

	/**
	 * 混合key构造函数
	 * @param grpType    分组方式
	 * @param zoneName   区域名称-可选。如果分组按照区域/混合 则不可空
	 * @param sex        性别-可选。如果分组按照性别/混合，则不可空
	 * @param salt       盐，可选。 如果非空，会根据分组方式夹在分组key后面。如果grpType是混合，额只加在区域之后
	 */
	public OrderMixedKey(String grpType, String zoneName, String sex) {
		this.grpType = grpType;
		this.zoneName = zoneName;
		this.sex = sex;
	}

	public String getGrpType() {
		return grpType;
	}

	public void setGrpType(String grpType) {
		this.grpType = grpType;
	}

	public String getZoneName() {
		return zoneName;
	}

	public void setZoneName(String zoneName) {
		this.zoneName = zoneName;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public int hashCode() {
		if (grpType.equals(OrderCommon.GRP_TYPE_SEX)) {
			return Objects.hash(sex);
		} else if (grpType.equals(OrderCommon.GRP_TYPE_ZONE)) {
			return Objects.hash(zoneName);
		} else {
			return Objects.hash(grpType, zoneName, sex);
		}
	}

	/**
	 * 覆盖默认方法，以便在一个方法中针对不同的分组进行比较
	 */
	@Override
	public boolean equals(Object o) {
		boolean result = false;
		if (this == o) {
			result = true;
		} else if (o == null || getClass() != o.getClass()) {
			result = false;
		} else {
			OrderMixedKey that = (OrderMixedKey) o;
			if (grpType.equals(OrderCommon.GRP_TYPE_SEX)) {
				result = Objects.equals(sex, that.sex) && Objects.equals(grpType, that.grpType);
			} else if (grpType.equals(OrderCommon.GRP_TYPE_ZONE)) {
				result = Objects.equals(zoneName, that.zoneName) && Objects.equals(grpType, that.grpType);
			} else {
				result = Objects.equals(zoneName, that.zoneName) && Objects.equals(sex, that.sex)
						&& Objects.equals(grpType, that.grpType);
			}
		}
		return result;

	}
}
